package org.looom.orglooomschool;

import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class Main_sharedata2 extends AppCompatActivity {
    private EditText editTextFileContent;
    private Button buttonWriteFile;
    private Button buttonReadFile;
    private TextView textViewReadFileContent;

    // 内部存储的文件名
    private static final String FILENAME = "my_data.txt";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sharedata2);

        // 获取布局中的控件实例
        editTextFileContent = findViewById(R.id.editTextFileContent);
        buttonWriteFile = findViewById(R.id.buttonWriteFile);
        buttonReadFile = findViewById(R.id.buttonReadFile);
        textViewReadFileContent = findViewById(R.id.textViewReadFileContent);

        // 设置写入文件按钮的点击监听器
        buttonWriteFile.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                writeFile();
            }
        });

        // 设置读取文件按钮的点击监听器
        buttonReadFile.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                readFile();
            }
        });
    }

    // 写入文件方法
    private void writeFile() {
        // 获取用户在输入框中输入的文本内容
        String content = editTextFileContent.getText().toString().trim();
        // 声明一个文件输出流对象，用于写入原始字节到文件
        FileOutputStream fos = null;

        try {
            // 获取FileOutputStream，使用MODE_PRIVATE模式
            fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
            // 使用OutputStreamWriter包装FileOutputStream，以字符流方式写入
            OutputStreamWriter osw = new OutputStreamWriter(fos);
            osw.write(content);
            osw.close();
            Toast.makeText(this, "文件写入成功", Toast.LENGTH_SHORT).show();
        } catch (IOException e) {
            e.printStackTrace();
            Toast.makeText(this, "文件写入失败: " + e.getMessage(), Toast.LENGTH_SHORT).show();
        } finally {
            // 确保流被关闭
            try {
                if (fos != null) {
                    fos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    // 读取文件方法
    private void readFile() {
        FileInputStream fis = null;
        StringBuilder sb = new StringBuilder();
        try {
            // 获取FileInputStream
            fis = openFileInput(FILENAME);
            // 使用BufferedReader包装FileInputStream，以字符流方式读取
            BufferedReader br = new BufferedReader(new InputStreamReader(fis));
            String line;
            while ((line = br.readLine()) != null) {
                sb.append(line).append("\n");
            }
            br.close();
            // 将读取到的内容显示在TextView中
            textViewReadFileContent.setText(sb.toString());
            Toast.makeText(this, "文件读取成功", Toast.LENGTH_SHORT).show();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            textViewReadFileContent.setText("文件不存在");
            Toast.makeText(this, "文件不存在", Toast.LENGTH_SHORT).show();
        } catch (IOException e) {
            e.printStackTrace();
            textViewReadFileContent.setText("文件读取失败: " + e.getMessage());
            Toast.makeText(this, "文件读取失败", Toast.LENGTH_SHORT).show();
        } finally {
            // 确保流被关闭
            try {
                if (fis != null) {
                    fis.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
